<?php
// 列出目录下的所有目录
// 只列出一级
    function list_folder($location = '/', $type = '*')
    {
        $dirs = array();
        if ($handle = opendir($location)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    $dirs[] = $file;
                }
            }
            closedir($handle);
        }
        // 再根据文件后缀名进行过滤
        $dirs = array_filter($dirs, function($item) use ($type) {
            return strpos($item, $type) !== false;
        });

        return $dirs;
    }

// 示例调用
//$location = 'E:/site/athlon_erp/uploadfiles/craft2tiangong';
//$type = '.jpg';
//$result = list_folder($location, $type);
//print_r($result);
?>


<?php



    $allowedOrigins = [
        'http://t.oa.com',
        'http://oa.enpingshi.com'
    ];

    $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';

    if (in_array($origin, $allowedOrigins)) {
        header("Access-Control-Allow-Origin: $origin");
    }

    header("Access-Control-Allow-Methods: POST");
    header("Access-Control-Allow-Headers: Content-Type");




    //这是一个接口，被ajax调用就好。


    //哪些是需要传递的参数
    //page_id  accesstoken   message  photo_url[]



    //104932918958782 = wedding invitation
    $pageId          = $_REQUEST ['page_id'];
    $pageAccessToken = $_REQUEST ['accesstoken'];
    $message         = $_REQUEST['content'];
    $media           = $_REQUEST['media'];
    $file_number     = $_REQUEST['file_number'];



    $media_number=$file_number;
    $media_folder=$media;


    $date = $_REQUEST['date'];
    //转换成UNIX 时间戳
    $date = strtotime($date);


    //创建单张照片的数组，值为600001~600500
    $media_list=array();
    for($i=1;$i<$media_number;$i++){
        $media_list[]=600000+$i;
    }





    $photoUrls = [
        "http://oa.enpingshi.com/uploadfiles/craft2tiangong/{$media_folder}/{$media_list[rand(1,$media_number)]}.jpg",
        "http://oa.enpingshi.com/uploadfiles/craft2tiangong/{$media_folder}/{$media_list[rand(1,$media_number)]}.jpg",
        "http://oa.enpingshi.com/uploadfiles/craft2tiangong/{$media_folder}/{$media_list[rand(1,$media_number)]}.jpg",
        "http://oa.enpingshi.com/uploadfiles/craft2tiangong/{$media_folder}/{$media_list[rand(1,$media_number)]}.jpg",
        "http://oa.enpingshi.com/uploadfiles/craft2tiangong/{$media_folder}/{$media_list[rand(1,$media_number)]}.jpg",
        "http://oa.enpingshi.com/uploadfiles/craft2tiangong/{$media_folder}/{$media_list[rand(1,$media_number)]}.jpg",
    ];


// 上传图片并获取 media_fbid
    $mediaFbids = [];
    foreach ($photoUrls as $url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v22.0/$pageId/photos");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
            "url"          => $url,
            "published"    => "false",
            "access_token" => $pageAccessToken
        ]));

        $response = curl_exec($ch);
        $data     = json_decode($response, true);

        if (isset($data['id'])) {
            $mediaFbids[] = $data['id'];
        } else {
            echo "Error uploading photo: " . json_encode($data) . "\n";
        }

        curl_close($ch);
    }

// 创建帖子并附加多张图片
    if (!empty($mediaFbids)) {
        $attachedMedia = json_encode(array_map(function ($id) {
            return ["media_fbid" => $id];
        }, $mediaFbids));

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v22.0/$pageId/feed");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
            "message"        => $message,
            "attached_media" => $attachedMedia,
            "published"    => "false",
            "scheduled_publish_time" => $date,
            "access_token"   => $pageAccessToken
        ]));

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        if ($httpCode == 200) {
            $responseData = json_decode($response, true);
            if (isset($responseData['id'])) {
                echo "Post successfully created with ID: " . $responseData['id'] . "\n";
            } else {
                echo "Post created, but no ID returned. Response: " . $response . "\n";
            }
        } else {
            echo "Failed to create post. HTTP Code: " . $httpCode . " Response: " . $response . "\n";
        }

        curl_close($ch);

    }



